package org.example.auth.controller;

import org.example.Result;
import org.example.auth.LoginException;
import org.example.auth.service.IMenuService;
import org.example.auth.service.TokensService;
import org.example.auth.vo.LoginVO;
import org.example.auth.vo.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {
    @Autowired
    private TokensService tokenService;
    @Autowired
    private IMenuService menuService;

    @PostMapping("/login")
    public Result<Token> login(@RequestBody LoginVO loginVO)  {
        try {
            Token token = tokenService.create(loginVO);
            return Result.ok(token);
        } catch (LoginException e) {
               return Result.error(e.getMessage());
        }
    }

    @GetMapping("/logout")
    public Result logout(@RequestHeader("token") String token) {
        return tokenService.logout(token) ? Result.ok() : Result.error();
    }
}
